//
//  MallViewController.m
//  MeterMart
//    [UserModel sharedInstanced].userID = @"ortm8wbLk0AfQsaRFK2mnZW2HLp8";
//  Created by ENERGY on 2018/9/16.
//  Copyright © 2018年 ENERGY. All rights reserved.
//  极光：Master Secret：32edef300e76f776988be24e    AppKey：32bc66ca71abe992af611dfa

#import "MallViewController.h"
#import "MallProductDetailViewController.h"
#import <SDCycleScrollView/SDCycleScrollView.h>
#import "CircleCollectionViewCell.h"
#import "ClassifyCollectionViewCell.h"
#import "RecommendTableViewCell.h"
#import "SearchViewController.h"
#import <CoreLocation/CoreLocation.h>
#import "UpdataAlertView.h"
#import "RecommendCollectionViewCell.h"
#import "ClassifyListViewController.h"
#import "SSKeychain.h"
#import "OtherListTableViewCell.h"
#import "HotelViewController.h"
#define circleImgW (SCREEN_WIDTH - 70/WIDTH_6_SCALE)/5

@interface MallViewController ()<UINavigationControllerDelegate, UITableViewDelegate, UITableViewDataSource, SDCycleScrollViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource, UITextFieldDelegate, CLLocationManagerDelegate>
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, strong)UIView * navBG;//搜索栏背景
@property (nonatomic, strong)SDCycleScrollView * cycleScrollView;
@property (nonatomic, strong)UIView * headerView;
@property (nonatomic, strong)UICollectionView * collectionView;
@property (nonatomic, assign)int currentPage;
@property (nonatomic, strong)NSMutableArray * classifyAreaArr;//为你推荐分类
@property (nonatomic, strong)NSMutableArray * qualityAreaArr;//轮播下分类
@property (nonatomic, strong)NSMutableArray * adAreaArr;//广告
@property (nonatomic, strong)NSMutableArray * produceArr;//
@property (nonatomic, strong)NSMutableArray * cycleArr;//轮播图
@property (nonatomic, strong)UITextField * searchTextField;//搜索栏
@property (nonatomic, strong)NSString * searchPlaceHolder;//搜索栏占位符

@property (nonatomic, strong)CLLocationManager *locationmanager;//定位服务
@property (nonatomic, strong)NSString *currentCity;//当前城市
@property (nonatomic, strong)NSString *strlatitude;//经度
@property (nonatomic, strong)NSString *strlongitude;//纬度
@property (nonatomic, strong)UpdataAlertView * updataAlertView;
@property (nonatomic, strong)UIView * headBottomGrayView;
@property (nonatomic, strong)UICollectionView * classifyCollectionView;

@property (nonatomic, strong)NSDictionary * topModel;
@property (nonatomic, strong)NSDictionary * bottomModel;
@property (nonatomic, strong)NSString * typeID;//推荐typeID
@property (nonatomic, strong)NSString * replaceID;
@property (nonatomic, assign)int collectionCellLineNum;
@property (nonatomic, strong)UICollectionViewFlowLayout * layOut;
- (NSString *)getDeviceUUID;//获取唯一标识符
- (void)judgeCollectinoH;
@end

@implementation MallViewController (networking)
//上部分数据
- (void)getHomePageData{
    [self showprogressHUD];
    [[AFAppDotNetAPIClient sharedClient] POST:@"indexiosApi/index_new" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if ([responseObject[@"sign"] isEqualToString:@"1"]) {
            self.topModel = [NSDictionary dictionaryWithDictionary:responseObject[@"list"]];
            [self judgeCollectinoH];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}
//推荐列表
- (void)getRecommendData{
    NSDictionary * dic = @{
                           @"only":[self getDeviceUUID],
                           };
    [self showprogressHUD];
    [[AFAppDotNetAPIClient sharedClient] POST:@"listiosApi/homepage" parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if ([responseObject[@"sign"] isEqualToString:@"1"]) {
            [self.produceArr removeAllObjects];
            [self.produceArr addObjectsFromArray:responseObject[@"list"][@"unfinequality"]];
            [self.tableView reloadData];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}
//上传位置
- (void)uploadLocationLongitude:(NSString *)longitude withLatitude:(NSString *)latitude{
    NSDictionary * dic = @{
                           @"only":[self getDeviceUUID],
                           @"longitude":longitude,
                           @"latitude":latitude,
                           };
    [self showprogressHUD];
    [[AFAppDotNetAPIClient sharedClient] POST:@"listApi/update_location" parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if ([responseObject[@"sign"] isEqualToString:@"1"]) {
            
        }else{
            
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self showError:error];
        [self hiddenProgressHUD];
    }];
}
@end
@implementation MallViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.delegate = self;
    [self setStatusBarBackgroundColor:[UIColor whiteColor]];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [self.updataAlertView cancelView];
}
- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleDefault;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.currentPage = 1;
    self.typeID = @"1";
    self.view.backgroundColor = [UIColor whiteColor];
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self.view addSubview:self.navBG];
    [self.view addSubview:self.tableView];
    
//    [self getHomePageData];//网络数据
//    
//    [self getRecommendData];
//    [self getLocation];
}

#pragma mark - init
- (void)setTopModel:(NSDictionary *)topModel{
    _topModel = topModel;
    [self.qualityAreaArr removeAllObjects];
    [self.adAreaArr removeAllObjects];
    [self.cycleArr removeAllObjects];
    [self.classifyAreaArr removeAllObjects];
    [self.qualityAreaArr addObjectsFromArray:topModel[@"info"]];
//    [self.adAreaArr addObjectsFromArray:topModel[@"ads"]];
    [self.classifyAreaArr addObjectsFromArray:topModel[@"search"]];
    [self.cycleArr addObjectsFromArray:topModel[@"sowingWap"]];
    NSMutableArray * cycleArrURL = @[].mutableCopy;
    for (int i = 0; i<self.cycleArr.count; i++) {
        [cycleArrURL addObject:[self.cycleArr[i] objectForKey:@"imgurl"]];
    }
    self.cycleScrollView.imageURLStringsGroup = cycleArrURL;
    NSMutableArray * tempArr = @[].mutableCopy;
    for (int i = 0; i<self.classifyAreaArr.count; i++) {
        NSMutableDictionary * dic = [NSMutableDictionary dictionaryWithDictionary:self.classifyAreaArr[i]];
        if (i == 0) {
            [dic setObject:@"1" forKey:@"ifSelect"];
        }else{
            [dic setObject:@"0" forKey:@"ifSelect"];
        }
        [tempArr addObject:dic];
    }
    self.searchTextField.placeholder = topModel[@"searchword"];
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    CFShow((__bridge CFTypeRef)(infoDictionary));
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    if (![app_Version isEqualToString:topModel[@"iosversion"]]) {
        [self.updataAlertView showView];
    }
    [self.classifyAreaArr removeAllObjects];
    [self.classifyAreaArr addObjectsFromArray:tempArr];
    [self.collectionView reloadData];
    [self.classifyCollectionView reloadData];
}
- (UpdataAlertView *)updataAlertView{
    if (!_updataAlertView) {
        _updataAlertView = [[UpdataAlertView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - BOTTOM_HEIGHT)];
    }return _updataAlertView;
}
- (NSMutableArray *)classifyAreaArr{
    if (!_classifyAreaArr) {
        _classifyAreaArr = @[].mutableCopy;
    }return _classifyAreaArr;
}
- (NSMutableArray *)qualityAreaArr{
    if (!_qualityAreaArr) {
        _qualityAreaArr = @[].mutableCopy;
    }return _qualityAreaArr;
}
- (NSMutableArray *)adAreaArr{
    if (!_adAreaArr) {
        _adAreaArr = @[].mutableCopy;
    }return _adAreaArr;
}
- (NSMutableArray *)produceArr{
    if (!_produceArr) {
        _produceArr = @[].mutableCopy;
    }return _produceArr;
}
- (NSMutableArray *)cycleArr{
    if (!_cycleArr) {
        _cycleArr = @[].mutableCopy;
    }return _cycleArr;
}
- (UIView *)navBG{
    if (!_navBG) {
        _navBG = [[UIView alloc] init];
        if (Is_Iphone_X) {
            _navBG.frame = CGRectMake(0, 40/WIDTH_6_SCALE, SCREEN_WIDTH, 44/WIDTH_6_SCALE);
        }else{
            _navBG.frame = CGRectMake(0, 20, SCREEN_WIDTH, 44/WIDTH_6_SCALE);
        }
        _navBG.backgroundColor = [UIColor whiteColor];
        //logo
        UIImageView * logoImg = [[UIImageView alloc] initWithFrame:CGRectMake(19.5/WIDTH_6_SCALE, 0, 65.5/WIDTH_6_SCALE, 15.5/WIDTH_6_SCALE)];
        logoImg.image = [UIImage imageNamed:@"Yimi"];
        logoImg.centerY = _navBG.height/2;
        [_navBG addSubview:logoImg];
        //搜索背景
        UIView * searchBGView = [[UIView alloc] initWithFrame:CGRectMake(logoImg.right + 10/WIDTH_6_SCALE, 0, SCREEN_WIDTH - logoImg.right - 20/WIDTH_6_SCALE - 20/WIDTH_6_SCALE, 28/WIDTH_6_SCALE)];
        searchBGView.centerY = logoImg.centerY;
        searchBGView.backgroundColor = [UIColor clearColor];
        searchBGView.layer.borderColor = getColor(@"221815").CGColor;
        searchBGView.layer.borderWidth = 1;
        searchBGView.layer.cornerRadius = 12;
        searchBGView.layer.masksToBounds = YES;
        [_navBG addSubview:searchBGView];
        //搜索栏
        self.searchTextField = [[UITextField alloc] initWithFrame:CGRectMake(5/WIDTH_6_SCALE, 1/WIDTH_6_SCALE, searchBGView.width - 10/WIDTH_6_SCALE, searchBGView.height - 2/WIDTH_6_SCALE)];
        self.searchTextField.font = DEF_FontSize_12;
        UIImageView *searchImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 14/WIDTH_6_SCALE, 14/WIDTH_6_SCALE)];
        searchImage.image = [UIImage imageNamed:@"magnifyingGlass"];
        self.searchTextField.leftView = searchImage;
        self.searchTextField.leftViewMode = UITextFieldViewModeAlways;
        self.searchTextField.delegate = self;
        [searchBGView addSubview:self.searchTextField];
    }return _navBG;
}
- (UIView *)headerView{
    if (!_headerView) {
        _headerView = [[UIView alloc] init];
        _headerView.backgroundColor = getColor(mainColor);
        [self setUpTableViewHeaderViewViews];//添加控件
        _headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.collectionView.bottom + 10/WIDTH_6_SCALE);
    }return _headerView;
}
- (void)setUpTableViewHeaderViewViews{
    //轮播图
    self.cycleScrollView = [[SDCycleScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 125/WIDTH_6_SCALE)];
    self.cycleScrollView.placeholderImage = [UIImage imageNamed:@"YimiPlaceHolder"];
    self.cycleScrollView.delegate = self;
    self.cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
    self.cycleScrollView.currentPageDotColor = [UIColor whiteColor];
    self.cycleScrollView.bannerImageViewContentMode = UIViewContentModeScaleToFill;
    self.cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
    __weak typeof (self)weakSelf = self;
    MallProductDetailViewController * mallProdeceDetailVC = [[MallProductDetailViewController alloc] init];
    HotelViewController * hotelVC = [[HotelViewController alloc] init];
    self.cycleScrollView.clickItemOperationBlock = ^(NSInteger index) {
        if ([[weakSelf.cycleArr[index] objectForKey:@"jumptype"] isEqualToString:@"1"]) {
            mallProdeceDetailVC.produceID = [weakSelf.cycleArr[index] objectForKey:@"goodsid"];
            mallProdeceDetailVC.hidesBottomBarWhenPushed = YES;
            [weakSelf.navigationController pushViewController:mallProdeceDetailVC animated:YES];
        }else{
            hotelVC.prodeceID = [weakSelf.cycleArr[index] objectForKey:@"goodsid"];
            hotelVC.typeID = [weakSelf.cycleArr[index] objectForKey:@"jumptype"];
            hotelVC.hidesBottomBarWhenPushed = YES;
            [weakSelf.navigationController pushViewController:hotelVC animated:YES];
        }
    };
    [_headerView addSubview:self.cycleScrollView];
    //collectionView
    self.layOut = [[UICollectionViewFlowLayout alloc] init];
    //第一个section：21为内边距，11为行距，90为item高度，73为广告高度，16为广告距header底部的距离
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.layOut];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerClass:[CircleCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([CircleCollectionViewCell class])];
    [self.collectionView registerClass:[ClassifyCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([ClassifyCollectionViewCell class])];
    [_headerView addSubview:self.collectionView];
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.navBG.bottom, SCREEN_WIDTH, SCREEN_HEIGHT - TABBAR_HEIGHT - self.navBG.height - self.navBG.top) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[RecommendTableViewCell class] forCellReuseIdentifier:NSStringFromClass([RecommendTableViewCell class])];
        [_tableView registerClass:[OtherListTableViewCell class] forCellReuseIdentifier:NSStringFromClass([OtherListTableViewCell class])];
        _tableView.backgroundColor = getColor(@"f7f7fb");
        _tableView.tableHeaderView = self.headerView;
    }return _tableView;
}
#pragma mark - tableViewDelegate和tableViewDatasource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = nil;
//    if ([self.typeID isEqualToString:@"1"]) {
    if ([[self.produceArr[indexPath.row] objectForKey:@"produceLevel"] isEqualToString:@"1"]) {
         RecommendTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([RecommendTableViewCell class]) forIndexPath:indexPath];
        cell.model = self.produceArr[indexPath.row];
        return cell;
    }else{
        OtherListTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([OtherListTableViewCell class]) forIndexPath:indexPath];
        cell.model = self.produceArr[indexPath.row];
        return cell;
    }return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    if ([self.typeID isEqualToString:@"1"]) {
    if ([[self.produceArr[indexPath.row] objectForKey:@"produceLevel"] isEqualToString:@"1"]) {
        return 150/WIDTH_6_SCALE;
    }else{
        return 172/WIDTH_6_SCALE;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.produceArr.count;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    if ([self.typeID isEqualToString:@"1"]) {
    if ([[self.produceArr[indexPath.row] objectForKey:@"produceLevel"] isEqualToString:@"1"]) {
        MallProductDetailViewController * vc = [[MallProductDetailViewController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        vc.produceID = [self.produceArr[indexPath.row] objectForKey:@"id"];
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        HotelViewController * vc = [[HotelViewController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        vc.prodeceID = [self.produceArr[indexPath.row] objectForKey:@"typeid_goods"];
        vc.navigationItem.title = [self.produceArr[indexPath.row] objectForKey:@"name"];
        vc.typeID = [self.produceArr[indexPath.row] objectForKey:@"type_id"];
        [self.navigationController pushViewController:vc animated:YES];
    }
}
#pragma mark - UICollectionViewDelegate,UICollectionViewDataSource
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell * cell = nil;
    if (collectionView == self.collectionView) {
        if (indexPath.section == 0) {
            if (self.qualityAreaArr.count > 6) {
                CircleCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([CircleCollectionViewCell class]) forIndexPath:indexPath];
                cell.model = self.qualityAreaArr[indexPath.row];
                return cell;
            }else{
                ClassifyCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ClassifyCollectionViewCell class]) forIndexPath:indexPath];
                cell.model = self.qualityAreaArr[indexPath.row];
                return cell;
            }
        }else if (indexPath.section == 1){
            ClassifyCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ClassifyCollectionViewCell class]) forIndexPath:indexPath];
            cell.adModel = self.adAreaArr[indexPath.row];
            return cell;
        }
    }else if (collectionView == self.classifyCollectionView){
        RecommendCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([RecommendCollectionViewCell class]) forIndexPath:indexPath];
        cell.model = self.classifyAreaArr[indexPath.row];
        return cell;
    }return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView == self.collectionView) {
        if (indexPath.section == 0) {
            if (self.qualityAreaArr.count > 6) {
                return CGSizeMake(circleImgW, circleImgW + 20/WIDTH_6_SCALE);
            }else{
                return CGSizeMake((SCREEN_WIDTH - 42/WIDTH_6_SCALE)/2, 90/WIDTH_6_SCALE);
            }
        }else{
            if (self.adAreaArr.count > 0) {
                return CGSizeMake(SCREEN_WIDTH - 30/WIDTH_6_SCALE, 73/WIDTH_6_SCALE);
            }return CGSizeZero;
        }
    }else{
        return CGSizeMake(60/WIDTH_6_SCALE, self.classifyCollectionView.height - 8/WIDTH_6_SCALE);
    }
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    if (collectionView == self.collectionView) {
        return 2;
    }return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (collectionView == self.collectionView) {
        if (section == 0){
            return self.qualityAreaArr.count;
        }return self.adAreaArr.count;
    }return self.classifyAreaArr.count;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    if (collectionView == self.collectionView) {
        if (section == 0){
            return UIEdgeInsetsMake(21/WIDTH_6_SCALE, 10/WIDTH_6_SCALE, 0, 10/WIDTH_6_SCALE);
        }else{
            if (self.adAreaArr.count > 0) {
                return UIEdgeInsetsMake(16/WIDTH_6_SCALE, 15/WIDTH_6_SCALE, 18/WIDTH_6_SCALE, 15/WIDTH_6_SCALE);
            }else{
                return UIEdgeInsetsZero;
            }
        }
    }return UIEdgeInsetsMake(0/WIDTH_6_SCALE, 15/WIDTH_6_SCALE, 0/WIDTH_6_SCALE, 0/WIDTH_6_SCALE);
}
//上下间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    if (collectionView == self.collectionView) {
        if (section == 0){
            return 11/WIDTH_6_SCALE;
        }else{
            return 0;
        }
    }return 6/WIDTH_6_SCALE;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView == self.collectionView) {
        if (indexPath.section == 0){
            ClassifyListViewController * vc = [[ClassifyListViewController alloc] init];
            vc.hidesBottomBarWhenPushed = YES;
            vc.typeID = [self.qualityAreaArr[indexPath.row] objectForKey:@"id"];
            vc.navigationItem.title = [self.qualityAreaArr[indexPath.row] objectForKey:@"intro"];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }else{
        for (int i = 0; i<self.classifyAreaArr.count; i++) {
            if ([[self.classifyAreaArr[i] objectForKey:@"ifSelect"] isEqualToString:@"1"]) {
                [self.classifyAreaArr[i] setObject:@"0" forKey:@"ifSelect"];
            }
            [self.classifyAreaArr[indexPath.row] setObject:@"1" forKey:@"ifSelect"];
        }
        self.typeID = [self.classifyAreaArr[indexPath.row] objectForKey:@"id"];
        [self.classifyCollectionView reloadData];
        [self getRecommendData];//tableView内容
    }
}
#pragma mark - textFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    SearchViewController * vc = [[SearchViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    vc.placeHolder = self.searchTextField.placeholder;
    [self.navigationController pushViewController:vc animated:YES];
    [textField endEditing:YES];
}
- (void)getLocation{
    //判断定位功能是否打开
    if ([CLLocationManager locationServicesEnabled]) {
        self.locationmanager = [[CLLocationManager alloc]init];
        self.locationmanager.delegate = self;
//        [self.locationmanager requestAlwaysAuthorization];
        self.currentCity = [NSString new];
        [self.locationmanager requestWhenInUseAuthorization];
        //设置寻址精度
        self.locationmanager.desiredAccuracy = kCLLocationAccuracyBest;
        self.locationmanager.distanceFilter = 5.0;
        [self.locationmanager startUpdatingLocation];
    }
}
#pragma mark CoreLocation delegate (定位失败)
//定位失败后调用此代理方法
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    //设置提示提醒用户打开定位服务
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"允许定位提示" message:@"请在设置中打开定位" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"打开定位" style:UIAlertActionStyleDefault handler:nil];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:okAction];
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:YES completion:nil];
}
#pragma mark 定位成功后则执行此代理方法
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    CLLocation *newLocation = locations[0];
    CLLocationCoordinate2D oldCoordinate = newLocation.coordinate;
    NSLog(@"旧的经度：%f,旧的纬度：%f",oldCoordinate.longitude,oldCoordinate.latitude);
    NSString * uploadLongitude = [NSString stringWithFormat:@"%f",oldCoordinate.longitude];
    NSString * uploadLatitude = [NSString stringWithFormat:@"%f",oldCoordinate.latitude];
    [self uploadLocationLongitude:uploadLongitude withLatitude:uploadLatitude];
    [manager stopUpdatingLocation];
    CLGeocoder *geoCoder = [[CLGeocoder alloc]init];
    //旧址
    CLLocation * location = locations.lastObject;
    //反地理编码
    [geoCoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if (placemarks.count > 0) {
            CLPlacemark *placeMark = placemarks[0];
            self.currentCity = placeMark.locality;
            if (!self.currentCity) {
                self.currentCity = @"无法定位当前城市";
            }
            /*看需求定义一个全局变量来接收赋值*/
            NSLog(@"----%@",placeMark.country);//当前国家
            NSLog(@"%@",self.currentCity);//当前的城市
            //            NSLog(@"%@",placeMark.subLocality);//当前的位置
            //            NSLog(@"%@",placeMark.thoroughfare);//当前街道
            //            NSLog(@"%@",placeMark.name);//具体地址
            
        }
    }];
}
#pragma mark - 获取唯一标识符
- (NSString *)getDeviceUUID{
    NSString * currentDeviceUUIDStr = [SSKeychain passwordForService:@"com.liqiu"account:@"uuid"];
    if (currentDeviceUUIDStr == nil || [currentDeviceUUIDStr isEqualToString:@""]){
        NSUUID * currentDeviceUUID  = [UIDevice currentDevice].identifierForVendor;
        currentDeviceUUIDStr = currentDeviceUUID.UUIDString;
        currentDeviceUUIDStr = [currentDeviceUUIDStr stringByReplacingOccurrencesOfString:@"-" withString:@""];
        currentDeviceUUIDStr = [currentDeviceUUIDStr lowercaseString];
        [SSKeychain setPassword: currentDeviceUUIDStr forService:@"com.liqiu"account:@"uuid"];
    }return currentDeviceUUIDStr;
}

- (void)judgeCollectinoH{
    if (self.qualityAreaArr.count > 6) {
        self.collectionCellLineNum = (int)(self.qualityAreaArr.count - 1)/5 + 1;
        if (self.adAreaArr.count > 0) {
            self.collectionView.frame = CGRectMake(0, self.cycleScrollView.bottom, SCREEN_WIDTH, (21+11*(self.collectionCellLineNum - 1)+(circleImgW + 20)*self.collectionCellLineNum +16+18+73)/WIDTH_6_SCALE);
        }else{
            self.collectionView.frame = CGRectMake(0, self.cycleScrollView.bottom, SCREEN_WIDTH, (21+11*(self.collectionCellLineNum - 1)+(circleImgW + 20)*self.collectionCellLineNum+14)/WIDTH_6_SCALE);
        }
        self.headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.collectionView.bottom + 10/WIDTH_6_SCALE);
        [self.collectionView reloadData];
        [self.tableView reloadData];
    }else{
        self.collectionCellLineNum = (int)(self.qualityAreaArr.count + 1)/2;
        if (self.adAreaArr.count > 0) {
            self.collectionView.frame = CGRectMake(0, self.cycleScrollView.bottom, SCREEN_WIDTH, (21+11*(self.collectionCellLineNum - 1)+90*self.collectionCellLineNum +16+18+73)/WIDTH_6_SCALE);
        }else{
            self.collectionView.frame = CGRectMake(0, self.cycleScrollView.bottom, SCREEN_WIDTH, (21+11*(self.collectionCellLineNum - 1)+90*self.collectionCellLineNum+14)/WIDTH_6_SCALE);
        }
        self.headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.collectionView.bottom + 10/WIDTH_6_SCALE);
        [self.collectionView reloadData];
        [self.tableView reloadData];
    }
}
@end
